<?php

namespace app\common\server;

use think\Image;
use dh2y\qrcode\QRcode;

class Qcode
{

    /**
     * 生成二维码
     * @return void
     * @author: k
     * @time: 2023-05-09 16:49
     */
    public function setCode($url, $save_path)
    {
        $code = new QRcode();//创建二维码对象，必须得。因为这是面向对象思想，我写代码，写出一个二维码对象，二维码对象操作。也就是说我控制二维码对象来操作里面的变量方法.
        $code->png($url, $save_path, 8)->entry();
    }

    /**
     * 带背景的二维码
     * @param $codePath 二维码路径
     */
    public function setBgCode($share_code = '', $bg_image = '', $x = 0, $y = 0)
    {
        require_once('../vendor/topthink/think-image/src/Image.php');
        #水印开始
        $image = Image::open($bg_image);
        $image->water($share_code, [$x, $y])->save($share_code);
        return true;
    }

    /**
     * 生成带文字的二维码
     * @param $codePath  二维码路径
     * @param $parameter 文字
     * @return mixed
     */
    public function BgCodeImage($codePath = '', $parameter = '', $x = 0, $y = 0)
    {
        require_once('../vendor/topthink/think-image/src/Image.php');

        $wwwpath = dirname($_SERVER['SCRIPT_FILENAME']) . '/';
        $newCodePath = $wwwpath . $codePath;//二维码路径
//        #新图片路径
//        $new_path = 'storage/share/';
//        #完整图片路径
//        $new_save_path = $wwwpath . $new_path;
//        if (!file_exists($new_save_path)) {
//            mkdir($new_save_path);
//        }
//        $new_path_name = md5(md5(time() . rand(100000, 999999)) . md5(time())) . '.png';
//        $new_share_code = $new_save_path . $new_path_name;
        $image = Image::open($newCodePath);
        $font = $wwwpath . 'static/image/ziti.ttf';
        $image->text($parameter, $font, 26, '#FF6A06', [$x, $y])->save($newCodePath);
        return $codePath;
    }


}